import globals
from SendToNeighbours import send_message_to_neighbors
import copy

def evict_messages_for_user (mp, message, user) :

    print "Evicting " + user
    try :
        globals.globalCacheMessageListLock.acquire()

        cacheMessageListclone = copy.deepcopy(globals.globalCacheMessageList)

        #cacheMessageListclone = []
        #for item in globals.globalCacheMessageList :
        #    cacheMessageListclone.append(item)

        i = 0
        for item in cacheMessageListclone :


            if item.toUserName == user :
                globals.globalCacheMessageList.pop(i)
            if ',' in item.toUserName:

                temp = item.toUserName.split(',')
                print "Temp is : ",
                print temp
                if user in temp :
                    temp.remove(user)
                    item.toUserName=''
                    for d in temp:
                        item.toUserName=item.toUserName+d+','
                    item.toUserName=item.toUserName[0:(len(item.toUserName)-1)]
                    globals.globalCacheMessageList[i].toUserName=item.toUserName[0:(len(item.toUserName)-1)]
                    print "Item to user : " + item.toUserName
            i = i + 1

    finally :
        globals.globalCacheMessageListLock.release()
    # print 'Evict called'
    message.dest = None
    message.destArray = None
    send_message_to_neighbors (mp, message)
    #print globalCacheMessageList
    return
